package com.now.printer.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.now.printer.R;
import com.now.printer.bean.HelpBean;
import com.now.printer.ui.activity.HelpDetailActivity;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by xudong on 2021/7/4.
 */

public class HelpGridAdapter  extends BaseAdapter {

    private Context context;
    private LayoutInflater mlayoutInflater;

    private int pageType = 1;

    private List<HelpBean> beanList = new ArrayList<>();
    public HelpGridAdapter(Context context,List<HelpBean> beanList) {
        this.context = context;
        this.beanList = beanList;
        //利用LayoutInflate把控件所在的布局文件加载到当前类中
        mlayoutInflater=LayoutInflater.from(context);
    }

    public void setData(List<HelpBean> beanList) {
        this.beanList.clear();
        this.beanList.addAll(beanList);
        notifyDataSetChanged();
    }

    public void setType(int pageType){
        this.pageType = pageType;
    }
    @Override
    public int getCount() {
        return beanList.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            //填写ListView的图标和标题等控件的来源，来自于layout_list_item这个布局文件
            //把控件所在的布局文件加载到当前类中
            convertView = mlayoutInflater.inflate(R.layout.item_grid_top,null);
            //生成一个ViewHolder的对象
            holder = new ViewHolder();
            //获取控件对象
            holder.grid_title=convertView.findViewById(R.id.grid_title);
            holder.grid_content=convertView.findViewById(R.id.grid_content);
            holder.ll_div_item_click=convertView.findViewById(R.id.ll_div_item_click);

            convertView.setTag(holder);

        }else{
            holder = (ViewHolder) convertView.getTag();
        }
        holder.grid_title.setText(beanList.get(position).getName());
        holder.grid_content.setText(beanList.get(position).getContent());

        holder.ll_div_item_click.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(context, HelpDetailActivity.class);
                intent.putExtra(HelpDetailActivity.INTENT_KEY_TITLE,beanList.get(position).getName());
                intent.putExtra(HelpDetailActivity.INTENT_KEY_DETAIL_IMAGE,position);
                intent.putExtra(HelpDetailActivity.INTENT_KEY_PAGE_TYPE,pageType);

                context.startActivity(intent);
            }
        });
        return convertView;

    }

    //写一个静态的class,把layout_grid_item的控件转移过来使用
    static class ViewHolder{
        public TextView grid_title;
        public TextView grid_content;
        private LinearLayout ll_div_item_click;
    }


}
